package org.wabson.alfresco.translations;

import java.io.InputStream;
import java.util.logging.Logger;

import org.jets3t.service.S3ServiceException;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

/**
 * Load message bundle files from a ZIP/JAR file stored in Amazon S3 via the JetS3T library
 * 
 * @author wabson
 */
public class JetS3TJarLoader extends AbstractJarLoader implements JarBundleLoader
{
    private static final Logger logger = Logger.getLogger(JetS3TJarLoader.class.getName());
            
    /**
     * Classpath location of JAR file
     */
    protected String jarPath;
    
    private String awsAccessKey;
    
    private String awsSecretKey;
    
    private String bucketName;
    
    /**
     * Instantiate a new file system message loader with the given base location
     * 
     * @param jarPath	Class path location of the JAR file
     * @param dirPath	Directory within the JAR file to search for message bundles
     */
    public JetS3TJarLoader(String jarPath, String dirPath)
    {
        this.jarPath = jarPath;
        this.dirPath = dirPath;
    }
    
    public void init()
    {
        AWSCredentials awsCredentials = 
                new AWSCredentials(awsAccessKey, awsSecretKey);
        try
        {
            RestS3Service s3Service = new RestS3Service(awsCredentials);
            
            // Get the JAR file path
            logger.info("Connecting to S3 bucket " + bucketName + ", path " + jarPath);
            S3Object object = s3Service.getObject(bucketName, jarPath);
            InputStream jis = object.getDataInputStream();
            
            if (jis == null)
            {
                throw new IllegalArgumentException("The JAR file " + jarPath + " was not found");
            }
            init(jis);
        }
        catch (S3ServiceException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ServiceException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public String toString()
    {
        return "jar:" + jarPath + "!" + dirPath;
    }

    public String getJarPath()
    {
        return jarPath;
    }

    public void setJarPath(String jarPath)
    {
        this.jarPath = jarPath;
    }

	@Override
	public String getFileName()
	{
		return jarPath.lastIndexOf("/") > -1 ? jarPath.substring(jarPath.lastIndexOf("/") + 1) : jarPath;
	}

	@Override
	public InputStream getInputStream() throws BundleLoadingException
	{
        return this.getClass().getResourceAsStream(jarPath);
	}

    public String getAwsAccessKey()
    {
        return awsAccessKey;
    }

    public void setAwsAccessKey(String awsAccessKey)
    {
        this.awsAccessKey = awsAccessKey;
    }

    public String getAwsSecretKey()
    {
        return awsSecretKey;
    }

    public void setAwsSecretKey(String awsSecretKey)
    {
        this.awsSecretKey = awsSecretKey;
    }

    public String getBucketName()
    {
        return bucketName;
    }

    public void setBucketName(String bucketName)
    {
        this.bucketName = bucketName;
    }
}
